*********************************************************************************************************************
****Code to compiled data for replication analysis in,***************************************************************
****Who Votes for Populist Presidential Candidates? Differential Support among US-based Latin American Diasporas"****
****Konstantin Ash and Anca Turcu, forthcoming in Political Geography************************************************
*********************************************************************************************************************

***Compilation of Cross-Country Data
#delimit;
clear;
insheet using "Populism Latin America - Country-Election-Candidate.csv";
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;
drop if  usdiasporashare==.;
gen left_populist=0;
replace left_populist=1 if right==0 & gps_pop_bin==1;
gen right_populist=0;
replace right_populist=1 if right==1 & gps_pop_bin==1; 
gen populist_post_trump=post_trump*gps_pop_bin;
gen populist_incumbent=incumbency*gps_pop_bin;
save populist_cross_country,replace;

****Factor Citizenship and Years Since Arrival into Socialization Variable***;
factor veteran citizen year_since, pcf;
rotate;
predict socialization;

***Additional Variables***;
gen sub_national_missing_country=1 if country=="Mexico" | country=="Paraguay" | country=="Colombia" | country=="Bolivia";
recode  sub_national_missing_country (.=0);

gen sub_national_missing=1 if country=="Mexico" | country=="Paraguay" | country=="Colombia" | country=="Bolivia" | election_year=="Brazil." | election_year=="Brazil12.00815" | election_year=="Brazil12.61756" | election_year=="Costa Rica24.84985" | election_year=="Guatemala." | election_year=="Honduras14.01181";
recode  sub_national_missing (.=0);

gen education_socialization=education*socialization;

gen posttrump_socialization=post_trump*socialization;

gen age_arrival_socialization=age_arrival*socialization;

gen education_socialization=education*socialization;



***Compile Sub-national Data for Main Article****;
insheet using "Populism Latin America - Brazil.csv";
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save brazil_consulate,replace;
clear;
insheet using "Populism Latin America - Chile.csv", delimiter(";");
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save chile_consulate,replace;
clear;
insheet using "Populism Latin America - Costa Rica.csv";
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save costarica_consulate,replace;

clear;
insheet using "Populism Latin America - Peru.csv",delimiter(";");
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save peru_consulate,replace;
clear;


insheet using "Populism Latin America - Ecuador.csv";
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save ecuador_consulate,replace;
clear;

insheet using "Populism Latin America - Venezuela.csv",delimiter(";");
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save venezuela_consulate,replace;
clear;


use brazil_consulate;
append using chile_consulate;
append using costarica_consulate;
append using ecuador_consulate;
append using peru_consulate;
append using venezuela_consulate;
gen right_populist=right*gps_pop_bin;
egen election_year = concat(country preselectionyear);

factor citizen years_since, pcf;
rotate;
predict socialization;

gen incumbency=0;
replace incumbency=1 if candidate=="Bolsonaro" & preselectionyear==2022;
replace incumbency=0.75 if candidate=="Rousseff" & preselectionyear==2010;
replace incumbency=1 if candidate=="Rousseff" & preselectionyear==2014;
replace incumbency=0.5 if candidate=="Lula";
replace incumbency=0.75 if candidate=="Piñera";
replace incumbency=0.75 if candidate=="Alvarado Quesada";
replace incumbency=0.5 if candidate=="Gutiérrez";
replace incumbency=0.75 if candidate=="Moreno";
replace incumbency=0.5 if candidate=="Garcia";
replace incumbency=1 if candidate=="Chávez";
replace incumbency=0.75 if candidate=="Maduro" & preselectionyear==2013;

gen age_arrival_socialization=age_arrival*socialization;

save sub_national,replace;


***Compile additional sub-national data***;
clear;
insheet using "Populism Latin America - Dom Rep.csv";
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save dr_consulate,replace;

clear;
insheet using "Populism Latin America - Honduras.csv",delimiter(";");
drop sources;
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;

save honduras_consulate,replace;
clear;

use sub_national;
append using dr_consulate;
append using honduras_consulate;
replace right_populist=right*gps_pop_bin;
drop election_year;
egen election_year = concat(country preselectionyear);

drop socialization;
factor citizen years_since, pcf;
rotate;
predict socialization;

replace incumbency=0 if incumbency==.;
replace incumbency=0.5 if candidate=="Fernández" & preselectionyear==2004;
replace incumbency=1 if candidate=="Fernández" & preselectionyear==2008;
replace incumbency=0.75 if candidate=="Medina" & preselectionyear==2012;
replace incumbency=1 if candidate=="Medina" & preselectionyear==2016;
replace incumbency=0.5 if candidate=="Mejia" ;
replace incumbency=0.75 if candidate=="Hernández" ;
replace populist_incumbent=incumbency*gps_pop_bin;
save sub_national_appendix,replace;


***Compile test of Systematic Missingness of Subnational Data***;
clear;
use populist_cross_country;
collapse (max) sub_national_missing  sub_national_missing_country age age_arrival female education log2_income unemployed veteran citizen race_id_white english_prof  year_since_arrival, by(country  preselectionyear);
save sub_national_missing,replace;
factor veteran citizen year_since, pcf;
rotate;
predict socialization;
save sub_national_missing, replace;



*****Compile Cross-Diaspora Data*****;
clear;
insheet using "Populist Latin America - Cross-Diaspora.csv";
save populist_cross_diaspora,replace;
egen election_year= concat(country  preselectionyear);
replace  gps_econ_l_r="1" if  gps_econ_l_r=="right";
replace  gps_econ_l_r="0" if  gps_econ_l_r=="left";
replace   gps_social_l_c="1" if   gps_social_l_c=="conservative";
replace   gps_social_l_c="0" if  gps_social_l_c=="liberal";
destring gps_econ_l_r, replace;
destring gps_social_l_c,replace;
rename  gps_econ_l_r right;
rename  gps_social_l_c conservative;
gen left_populist=0;
replace left_populist=1 if right==0 & gps_pop_bin==1;
gen right_populist=0;
replace right_populist=1 if right==1 & gps_pop_bin==1;
gen post_populist=.;
replace post_populist=1 if preselectionyear>2014 &  dest_country=="United States"; 
replace post_populist=1 if preselectionyear>2022 &  dest_country=="Argentina"; 
replace post_populist=1 if preselectionyear>2017 &  dest_country=="Brazil"; 
replace post_populist=1 if preselectionyear>2012 &  dest_country=="Italy"; 
replace post_populist=1 if preselectionyear>2014 &  dest_country=="Spain"; 
replace post_populist=0 if post_populist==.;
gen populist_post_populist=post_populist*gps_pop_bin;
gen populist_incumbent=incumbency*gps_pop_bin;
gen spain=0;
replace spain=1 if dest_country=="Spain"; 
gen argentina=0;
replace argentina=1 if dest_country=="Argentina"; 
gen italy=0;
replace italy=1 if dest_country=="Italy"; 
gen brazil=0;
replace brazil=1 if dest_country=="Brazil"; 
gen canada=0;
replace canada=1 if dest_country=="Canada";

gen cross_diaspora=1 if country=="Ecuador" |country=="Peru" | country=="Paraguay" | country=="Chile" | country=="Bolivia";
recode  cross_diaspora (.=0);

gen sub_national_missing=1 if country=="Mexico" | country=="Paraguay" | country=="Colombia" | country=="Bolivia";
recode  sub_national_missing (.=0);
 
save populist_cross_diaspora,replace;
